package manager;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import util.CurrentUser;
import data.dao.*;
import data.dto.*;
import data.searchdto.*;
import data.dao.daoFactory.*;
import data.dao.daoImplementation.NotFoundException;
import data.dao.daoImplementation.SQLException;

public class UserManager {
	private UserDAO userDAO;
	private UserTypeDAO usertypeDAO;
	private UserHierarchyDAO userHierarchyDAO;
	private UserHierarchyDetailDAO userHierarchyDetailDAO;

	private ArrayList<UserDTO> userList;
	private ArrayList<userTypeDTO> usertypeList;

	public UserManager() {
		this.userDAO = UserFactory.loadInstance().getUserDAO();
		this.usertypeDAO = UserFactory.loadInstance().getUserTypeDAO();
		this.userHierarchyDAO = UserFactory.loadInstance()
				.getUserHierarchyDAO();
		this.userHierarchyDetailDAO = UserFactory.loadInstance()
				.getUserHierarchyDetailDAO();
	}

	public UserDTO findUser(UserSearchDTO usersearchdto)
			throws UserDAOException {
		UserDTO user = new UserDTO();
		try {
			user = userDAO.findUser(usersearchdto);
		} catch (UserDAOException e) {
			e.printStackTrace();
		}
		return user;
	}

	public ArrayList<UserDTO> findAllUser() throws UserDAOException {
		userList = new ArrayList<UserDTO>();
		try {
			userList = userDAO.findAllUser();
		} catch (UserDAOException e) {
			e.printStackTrace();
		}
		return userList;
	}

	public void insertLeaveDetail(UserDTO User) throws UserDAOException {
		try {
			userDAO.insertUserDetail(User);
		} catch (UserDAOException e) {
			e.printStackTrace();
		}

	}

	public void updateLeaveDetail(UserDTO User) throws UserDAOException {
		try {
			userDAO.updateUserDetail(User);
		} catch (UserDAOException e) {
			e.printStackTrace();
		}

	}

	public void deleteLeaveDetail(UserDTO User) throws UserDAOException,
			NotFoundException, SQLException {
		try {
			userDAO.deleteUserDetail(User);
		} catch (UserDAOException e) {
			e.printStackTrace();
		}

	}

	// added by Taufin
	public boolean login(String userName, String password, boolean isAdmin, HttpSession session) {
		boolean isLogin = false;

		try {
			UserDTO user = userDAO.findUser(userName, password);
			if (user != null) {
				userTypeDTO userType = usertypeDAO
						.findUserType(new UserTypeSearchDTO(user.getUserType(),
								""));
				
				if (isAdmin){
					if (userType.getName().equalsIgnoreCase("admin")) {
						isLogin = true;
					}
				}
				else{
					if (!userType.getName().equalsIgnoreCase("admin")) {
						isLogin = true;
						if (userType.getName().toLowerCase().indexOf("manager") > -1){
							session.setAttribute("ismanager", true);
						}
					}
				}
				
				if (isLogin) {
					session.setAttribute("user", user);
				}
			}
		} catch (UserDAOException e) {
			e.printStackTrace();
		}

		return isLogin;
	}

	public String getDefaultPage(HttpSession session) {
		String link = "";
		UserDTO currentUser = util.CurrentUser.getCurrentUser(session);

		try {
			UserDTO user = userDAO.findUser(new UserSearchDTO(
					(short) currentUser.getId(), null, null, 0, 0));

			userTypeDTO userType = usertypeDAO
					.findUserType(new UserTypeSearchDTO(user.getUserType(), ""));

			// admin
			if (userType.getName().equalsIgnoreCase("admin")) {
				link = "/AdminStaffServlet";
			}
			// any staff
			else if (userType.getName().toLowerCase().indexOf("staff") > -1) {
				link = "/ManagePersonalLeaveServlet";
			}
			// any manager
			else if (userType.getName().toLowerCase().indexOf("manager") > -1) {
				link = "/ViewLeaveServlet";
			}
		} catch (UserDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return link;
	}

	public UserHierarchyDTO findUserHierarchy(
			UserHierarchySearchDTO userHierarchySearchDTO) {
		UserHierarchyDTO userHierarchyDTO = null;

		try {
			userHierarchyDTO = userHierarchyDAO
					.findHierarchy(userHierarchySearchDTO);
		} catch (UserHierarchyDAOException e) {
			e.printStackTrace();
		}

		return userHierarchyDTO;
	}

	public ArrayList<UserHierarchyDTO> findAllUserHierarchy() {
		ArrayList<UserHierarchyDTO> list = new ArrayList<UserHierarchyDTO>();

		try {
			list = userHierarchyDAO.findAllHierarchy();
		} catch (UserHierarchyDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}

	private boolean isUserHierarchyExist(UserHierarchyDTO userHierarchy) {
		boolean isExist = false;

		UserHierarchySearchDTO userHierarchySearch = new UserHierarchySearchDTO();
		userHierarchySearch.setUserType(userHierarchy.getUserType());

		if (this.findUserHierarchy(userHierarchySearch) != null) {
			isExist = true;
		}

		return isExist;
	}

	public void insertUserHierarchy(UserHierarchyDTO userHierarchy) {
		try {
			if (!this.isUserHierarchyExist(userHierarchy)) {
				userHierarchyDAO.insertHierarchy(userHierarchy);
			}
		} catch (UserHierarchyDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void updateUserHierarchy(UserHierarchyDTO userHierarchy) {
		try {
			if (this.isUserHierarchyExist(userHierarchy)) {
				userHierarchyDAO.updateHierarchy(userHierarchy);
			}
		} catch (UserHierarchyDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void deleteUserHierarchy(UserHierarchyDTO userHierarchy) {
		try {
			userHierarchyDAO.deleteHierarchy(userHierarchy);
		} catch (UserHierarchyDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public UserHierarchyDetailDTO findUserHierarchyDetail(
			UserHierarchyDetailSearchDTO userHierarchyDetailSearchDTO) {
		UserHierarchyDetailDTO userHierarchyDetailDTO = null;

		try {
			userHierarchyDetailDTO = userHierarchyDetailDAO
					.findHierarchyDetail(userHierarchyDetailSearchDTO);
		} catch (UserHierarchyDAOException e) {
			e.printStackTrace();
		}

		return userHierarchyDetailDTO;
	}

	public ArrayList<UserHierarchyDetailDTO> findAllHierarchyDetail(
			UserHierarchyDetailSearchDTO userHierarchyDetailSearch) {
		ArrayList<UserHierarchyDetailDTO> list = new ArrayList<UserHierarchyDetailDTO>();

		try {
			list = userHierarchyDetailDAO
					.findAllHierarchyDetail(userHierarchyDetailSearch);
		} catch (UserHierarchyDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}

	private boolean isUserHierarchyDetailExist(
			UserHierarchyDetailDTO userHierarchyDetail) {
		boolean isExist = false;

		UserHierarchyDetailSearchDTO userHierarchyDetailSearch = new UserHierarchyDetailSearchDTO();
		userHierarchyDetailSearch
				.setUserType(userHierarchyDetail.getUserType());
		userHierarchyDetailSearch.setHierarchyId(userHierarchyDetail
				.getHierarchyId());

		if (this.findUserHierarchyDetail(userHierarchyDetailSearch) != null) {
			isExist = true;
		}

		return isExist;
	}

	public void insertUserHierarchyDetail(
			UserHierarchyDetailDTO userHierarchyDetail) {
		try {
			if (!isUserHierarchyDetailExist(userHierarchyDetail)) {
				userHierarchyDetailDAO
						.insertHierarchyDetail(userHierarchyDetail);
			}
		} catch (UserHierarchyDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void updateUserHierarchyDetail(
			UserHierarchyDetailDTO userHierarchyDetail) {
		try {
			if (isUserHierarchyDetailExist(userHierarchyDetail)) {
				userHierarchyDetailDAO
						.updateHierarchyDetail(userHierarchyDetail);
			}
		} catch (UserHierarchyDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void deleteUserHierarchyDetail(
			UserHierarchyDetailDTO userHierarchyDetail) {
		try {
			userHierarchyDetailDAO.deleteHierarchyDetail(userHierarchyDetail);
		} catch (UserHierarchyDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// =================

	public userTypeDTO findUserType(UserTypeSearchDTO usertypesearchdto)
			throws UserDAOException {
		userTypeDTO usertype = null;
		try {
			usertype = usertypeDAO.findUserType(usertypesearchdto);
		} catch (UserDAOException e) {
			e.printStackTrace();
		}
		return usertype;
	}

	public ArrayList<userTypeDTO> findAllUserType() throws UserDAOException {
		usertypeList = new ArrayList<userTypeDTO>();
		try {
			usertypeList = usertypeDAO.findAllUserType();
		} catch (UserDAOException e) {
			e.printStackTrace();
		}
		return usertypeList;
	}

	public ArrayList<userTypeDTO> findAllUserType(
			ArrayList<UserHierarchyDetailDTO> lists) {
		usertypeList = new ArrayList<userTypeDTO>();
		try {
			for (UserHierarchyDetailDTO list : lists) {
				usertypeList.add(this.findUserType(new UserTypeSearchDTO(list
						.getUserType(), "")));
			}
		} catch (UserDAOException e) {
			e.printStackTrace();
		}
		return usertypeList;
	}

	public ArrayList<userTypeDTO> findAllUserTypeDisplay() {
		usertypeList = new ArrayList<userTypeDTO>();

		try {
			usertypeList = this.findAllUserType();
		} catch (UserDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		userTypeDTO userType = new userTypeDTO();
		userType.setId((short) 0);
		userType.setName("Choose Staff Type");

		usertypeList.add(0, userType);

		return usertypeList;
	}

	public void insertUserType(userTypeDTO User) throws UserDAOException {
		try {
			usertypeDAO.insertUserType(User);
		} catch (UserDAOException e) {
			e.printStackTrace();
		}
	}

	public void updateUserType(userTypeDTO User) throws UserDAOException {
		try {
			usertypeDAO.updateUserType(User);
		} catch (UserDAOException e) {
			e.printStackTrace();
		}
	}

	public void deleteUserType(userTypeDTO User) throws UserDAOException,
			NotFoundException, SQLException {
		try {
			usertypeDAO.deleteUserType(User);
		} catch (UserDAOException e) {
			e.printStackTrace();
		}
	}
}
